package br.com.sge.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import br.com.sge.entidades.Aluno;
import br.com.sge.entidades.Disciplina;
import br.com.sge.entidades.Professor;

/**
 *
 * @author Gustavo Rodrigues Torre
 */
public class HibernateUtil {

    private static SessionFactory sessionFactory;

    private HibernateUtil() {
        
    }

    public static SessionFactory getSessionFactory() {

        if (sessionFactory == null) {
            try {
                AnnotationConfiguration ac = new AnnotationConfiguration();
                ac.addAnnotatedClass(Aluno.class);
                ac.addAnnotatedClass(Professor.class);
                ac.addAnnotatedClass(Disciplina.class);
                sessionFactory = ac.configure().buildSessionFactory();
                } catch (Throwable ex) {
                System.err.println("Iniciando SessionFactory falha." + ex);
                throw new ExceptionInInitializerError(ex);
            }

            return sessionFactory;

        } else {
            return sessionFactory;
        }
        
    }

    public static void main(String[] args) {
        HibernateUtil.getSessionFactory();
    }

}
